<?php

declare(strict_types=1);

namespace app\controller\v1;

use think\Request;
use app\service\OrderService;
use app\service\UserService;
use app\model\TicketOrders;
use app\model\TicketVerifications;


class OrderController extends BaseAPIController
{
    // 提交门票订单
    public function submitTicketOrders(Request $request)
    {


        $params = $this->request->post();
        // 验证参数

        $validate = validate([
            'tickets' => 'require',
            'payment_method' => 'require',
        ])->message([
            'tickets.require' => '门票不能为空',
            'payment_method.require' => '请选择支付',
        ]);
        if (!$validate->check($params)) {
            return $this->error($validate->getError());
        }
        try {
            $result = OrderService::submitTicketOrders($params);
            return $this->success($result, $result['msg']);
        } catch (\Exception $e) {
            return $this->error($e->getMessage(),400);
        }
    }

    //获取门票列表
    public function getTicketOrderList()
    {
        try {
            // 获取当前用户
            $userId = UserService::getCurrentUserId();
            // 获取分页参数
            $pageParam = $this->request->get('page') ?: 1;
            $pageSizeParam = $this->request->get('pageSize') ?: 10;
            $statusParam = $this->request->get('status');

            $status = $statusParam;
            $page = ((int)$pageParam - 1) * (int)$pageSizeParam;
            $result = OrderService::getTicketOrderList($userId, $page, (int)$pageSizeParam);
            return $this->success($result);
        } catch (\Exception $e) {
            return $this->error('获取订单列表失败：' . $e->getMessage());
        }
    }

    // 门票退款
    public function refundTicketOrder()
    {
        $result = '';
        try {
            // 获取当前用户
            $userId = UserService::getCurrentUserId();
            // 获取分页参数
            $params = $this->request->post();
            // 验证参数
            $validate = validate([
                'order_sn' => 'require',
            ])->message([
                'order_sn.require' => '订单不能为空',
            ]);
            if (!$validate->check($params)) {
                return $this->error($validate->getError());
            }
            $result = OrderService::refundTicketOrder($userId, $params);
        } catch (\Exception $e) {
            return $this->error('退款失败：' . $e->getMessage().$e->getLine(),400);
        }
        return $this->success($result);
    }

    //todo 核销，根据核销码查询订单，核销状态，核销数量等条件
    
    // 核销门票
    public function verifyTicket(Request $request)
    {
        $params = $this->request->post();
        
        // 验证参数
        $validate = validate([
            'verif_code' => 'require',
        ])->message([
            'verif_code.require' => '核销码不能为空',
        ]);
        
        if (!$validate->check($params)) {
            return $this->error($validate->getError());
        }
        
        try {
            $result = OrderService::verifyTicket($params['verif_code']);
            return $this->success($result, '核销成功');
        } catch (\Exception $e) {
            return $this->error($e->getMessage(), 400);
        }
    }
    



    // 创建商城订单
    public function createMallOrder(Request $request)
    {
        $params = $this->request->post();
        // 验证参数
        $validate = validate([
            'payment_type' => 'require',
            'address_id' => 'require',
        ])->message([
            'payment_type.require' => '请选择支付',
            'address_id.require' => '请选择地址',
        ]);
        if (!$validate->check($params)) {
            return $this->error($validate->getError(),400);
        }
        try {
            $result = OrderService::createMallOrder($params);
            return $this->success($result, '订单创建成功');
        } catch (\Exception $e) {
            return $this->error($e->getMessage(), 400);
        }
    }

    // 获取商城订单列表
    public function getMallOrderList()
    {
        try {
            // 获取当前用户
            $userId = UserService::getCurrentUserId();
            // 获取分页参数
            $pageParam = $this->request->get('page') ?: 1;
            $pageSizeParam = $this->request->get('pageSize') ?: 10;
            $statusParam = $this->request->get('status');

            $status = $statusParam;
            $page = ((int)$pageParam - 1) * (int)$pageSizeParam;
            $result = OrderService::getMallOrderList($userId, $page, (int)$pageSizeParam, $status);
            return $this->success($result);
        } catch (\Exception $e) {
            return $this->error('获取订单列表失败：' . $e->getMessage(),400);
        }
    }

    // 确认收货
    public function confirmMallOrderReceipt()
    {
        $params = $this->request->post();
        // 验证参数
        $validate = validate([
            'order_id' => 'require',
        ])->message([
            'order_id.require' => '订单不能为空',
        ]);
        if (!$validate->check($params)) {
            return $this->error($validate->getError());
        }
        try {
            $result = OrderService::confirmMallOrderReceipt($params['order_id']);
            return $this->success([], '确认收货成功');
        } catch (\Exception $e) {
            return $this->error($e->getMessage(), 400);
        }
    }


    // 申请退款
    public function applyMallOrderRefund()
    {
        $params = $this->request->post();
        // 验证参数
        $validate = validate([
            'order_id' => 'require',
        ])->message([
            'order_id.require' => '订单不能为空',
        ]);
        if (!$validate->check($params)) {
            return $this->error($validate->getError());
        }
        try {
            $result = OrderService::applyMallOrderRefund($params['order_id']);
            return $this->success($result['data'],$result['msg']);
        } catch (\Exception $e) {
            return $this->error($e->getMessage(), 400);
        }
    }
}
